package Dao.impl;

import Dao.TrainDao;
import entity.TrainVehicleInfo;
import untils.Msg;
import untils.SqlCollection;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

public class TrainDaoImpl implements TrainDao {
    private SqlCollection sqlCollection = new SqlCollection();
    @Override
    public ArrayList<TrainVehicleInfo> viewTrainM() {
        PreparedStatement pst = null;
        Connection conn = null;
        ResultSet rs = null;
        TrainVehicleInfo trainVehicleInfo = null;
        ArrayList<TrainVehicleInfo> trainVehicleInfos = new ArrayList<>();
        try {
            conn = sqlCollection.openConnection();
            String sql = "select * from trainvehicleinfo where logic_delete = ?";
            pst = conn.prepareStatement(sql);
            pst.setInt(1,0);
            rs = pst.executeQuery();

            while (rs.next()){
                trainVehicleInfo = new TrainVehicleInfo();
                trainVehicleInfo.setCarId(rs.getInt(1));
                trainVehicleInfo.setCarName(rs.getString(2));
                trainVehicleInfo.setCarType(rs.getString(3));
                trainVehicleInfo.setMakeDate(rs.getDate(4));
                trainVehicleInfo.setCurrentStatus(rs.getString(5));
                trainVehicleInfo.setRailwaybureau(rs.getString(6));
                trainVehicleInfo.setSeats(rs.getInt(7));
                trainVehicleInfo.setCapacity(rs.getInt(8));
                trainVehicleInfo.setLastMaintenanceDate(rs.getDate(9));
                trainVehicleInfos.add(trainVehicleInfo);
            }

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            sqlCollection.closeConnection(conn,pst,rs);
        }
        return trainVehicleInfos;
    }

    @Override
    public void perfectTrainM(TrainVehicleInfo t) {
        PreparedStatement pst = null;
        Connection conn = null;
        ResultSet rs = null;
        try{
            conn = sqlCollection.openConnection();
            String sql = "update trainvehicleinfo set cartype = ?,makeproduct = ?,makedate = ?,currentstatus = ?,railwaybureau = ?,capacity = ?,lastmaintenancedate = ? where carid = ?";
            pst = conn.prepareStatement(sql);
            pst.setString(1,t.getCarType());
            pst.setString(2,t.getMakeProduct());
            pst.setDate(3,t.getMakeDate());
            pst.setString(4,t.getCurrentStatus());
            pst.setString(5,t.getRailwaybureau());
            pst.setInt(6,t.getCapacity());
            pst.setDate(7,t.getLastMaintenanceDate());
            pst.setInt(8,t.getCarId());
            int res = pst.executeUpdate();
            if (res > 0){
                Msg.msg = "修改成功";
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            sqlCollection.closeConnection(conn,pst);
        }
    }

    @Override
    public void updateTrainM(TrainVehicleInfo t) {
        PreparedStatement pst = null;
        Connection conn = null;
        try{
            conn = sqlCollection.openConnection();
            String sql = "update trainvehicleinfo set makedate = ?,currentstatus = ?,lastmaintenancedate = ? where carid = ?";
            pst = conn.prepareStatement(sql);
            pst.setDate(1,t.getMakeDate());
            pst.setString(2,t.getCurrentStatus());
            pst.setDate(3,t.getLastMaintenanceDate());
            pst.setInt(4,t.getCarId());
            int res = pst.executeUpdate();
            if (res > 0){
                Msg.msg = "修改成功";
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            sqlCollection.closeConnection(conn,pst);
        }
    }

    @Override
    public void deleteTrainM(int carId) {
        PreparedStatement pst = null;
        Connection conn = null;
        try{
            conn = sqlCollection.openConnection();
            String sql = "update trainvehicleinfo set logic_delete = ? where carid = ?";
            pst = conn.prepareStatement(sql);
            pst.setInt(1,1);
            pst.setInt(2,carId);
            int res = pst.executeUpdate();
            if (res > 0){
                Msg.msg = "删除成功";
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            sqlCollection.closeConnection(conn,pst);
        }

    }
}
